<?php
include_once("../../untils/conn.php");

$proxy_id = $_POST['proxy_id'];
$oderid = $_POST['oderid'];
$price = $_POST['price'];
$type = $_POST['type'];
$notes = $_POST['notes'];
$sutime = time();

// 查询proxy表中proxy_sta等于$proxy_id的数据
$sql = "SELECT * FROM proxy WHERE proxy_id = $proxy_id";
$result = mysqli_query($con, $sql);

// 检查查询结果
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $proxy_acc = $row['proxy_acc'];
    $proxy_balance = $row['proxy_balance'];

    // 根据type进行不同的操作
    if ($type == 0) {
        $new_balance = $proxy_balance + $price;
        $update_sql = "UPDATE proxy SET proxy_balance = $new_balance WHERE proxy_id = $proxy_id";
        mysqli_query($con, $update_sql);
         // 在revenue表中新增数据
        $insert_sql = "INSERT INTO revenue (account, oderid, type, price, beizhu, fund_notes, oder_sta, state, cr_time, dz_time) VALUES ('$proxy_acc', '$oderid', $type, $price, '$notes', '$notes', 3, 2, $sutime, $sutime)";
        mysqli_query($con, $insert_sql);
    } elseif ($type == 1) {
        $new_balance = $proxy_balance - $price;
        $update_sql = "UPDATE proxy SET proxy_balance = $new_balance WHERE proxy_id = $proxy_id";
        mysqli_query($con, $update_sql);
         // 在revenue表中新增数据
        $insert_sql = "INSERT INTO revenue (account, oderid, type, price, beizhu, fund_notes, oder_sta, state, cr_time, dz_time) VALUES ('$proxy_acc', '$oderid', $type, $price, '$notes', '$notes', 3, 3, $sutime, $sutime)";
        mysqli_query($con, $insert_sql);
        }

    // 在revenue表中新增数据
    // $insert_sql = "INSERT INTO revenue (account, oderid, type, price, beizhu, fund_notes, oder_sta, state, cr_time, dz_time) VALUES ('$proxy_acc', '$oderid', $type, $price, '$notes', '$notes', 3, 3, $sutime, $sutime)";
    // mysqli_query($con, $insert_sql);

    // 执行成功后的操作
    echo "<script>alert('操作成功');setTimeout(function(){window.location.href='../proxylist.php';}, 1000);</script>";
} else {
    // 商品不存在的操作
    echo "<script>alert('操作失败');setTimeout(function(){window.location.href='../proxylist.php';}, 1000);</script>";
}

mysqli_close($con);
?>
